package com.utf999.qiangmanger.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.IStoreUserGroupDAO;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.entity.orm.OrderTypeEntity;
import com.utf999.qiangmanger.entity.orm.StoreUserEntity;
import com.utf999.qiangmanger.entity.orm.StoreUserGroupEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.ICompanyService;
import com.utf999.qiangmanger.service.iface.IOrderTypeService;
import com.utf999.qiangmanger.service.iface.IStoreUserGroupService;
import com.utf999.qiangmanger.service.iface.IStoreUserService;


/**
 * @author yinwenjie
 *
 */
@Component("StoreUserGroupServiceImpl")
public class StoreUserGroupServiceImpl extends AbstractService<StoreUserGroupEntity, IStoreUserGroupDAO> implements IStoreUserGroupService {
	
	/**
	 * 
	 */
	@Autowired
	private IStoreUserGroupDAO storeUserGroupDAO;
	
	@Autowired
	private ICompanyService companyService;
	
	@Autowired
	private IStoreUserService storeUserService;
	
	@Autowired
	private IOrderTypeService orderTypeService;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected IStoreUserGroupDAO getBaseDAO() {
		return this.storeUserGroupDAO;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#createStoreUserGroup(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void createStoreUserGroup(String companyid, String groupName, String groupRemark) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(groupName)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		CompanyEntity company = (CompanyEntity)this.companyService.getEntity(companyid);
		if(company == null) {
			throw new BizException("没有找到指定的公司信息。", ResponseCode.E403);
		}
		
		//开始添加
		StoreUserGroupEntity group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName(groupName);
		group.setGroupRemark(groupRemark);
		group.setGroupType(2);
		group.setStatus(1);
		
		this.insert(group);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#initDefaultStoreUserGroup(com.utf999.qiangmanger.entity.orm.CompanyEntity)
	 */
	@Override
	public void initDefaultStoreUserGroup(CompanyEntity company) throws BizException {
		//客服组
		StoreUserGroupEntity group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName("客服组");
		group.setGroupRemark("客服组");
		group.setGroupType(1);
		group.setStatus(1);
		this.insert(group);
		//技服组
		group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName("技服组");
		group.setGroupRemark("技服组");
		group.setGroupType(1);
		group.setStatus(1);
		this.insert(group);
		//保洁组
		group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName("保洁组");
		group.setGroupRemark("保洁组");
		group.setGroupType(1);
		group.setStatus(1);
		this.insert(group);
		//保安组
		group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName("保安组");
		group.setGroupRemark("保安组");
		group.setGroupType(1);
		group.setStatus(1);
		this.insert(group);
		//驻场组
		group = new StoreUserGroupEntity();
		group.setCompany(company);
		group.setCreateTime(new Date());
		group.setGroupName("驻场组");
		group.setGroupRemark("驻场组");
		group.setGroupType(1);
		group.setStatus(1);
		this.insert(group);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#modifyStoreUserGroup(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void modifyStoreUserGroup(String groupid, String groupName, String groupRemark) throws BizException {
		if(StringUtils.isEmpty(groupid) || StringUtils.isEmpty(groupName)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		StoreUserGroupEntity group = (StoreUserGroupEntity)this.getEntity(groupid);
		if(group == null ) {
			throw new BizException("没有找到对应的用户分组信息！", ResponseCode.E403);
		}
		
		group.setGroupName(groupName);
		group.setGroupRemark(groupRemark);
		this.update(group);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#modifyUserGroupStatus(java.lang.String)
	 */
	@Override
	public void modifyUserGroupStatus(String groupid) throws BizException {
		if(StringUtils.isEmpty(groupid)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		StoreUserGroupEntity group = (StoreUserGroupEntity)this.storeUserGroupDAO.getEntity(groupid);
		if(group == null ) {
			throw new BizException("没有找到对应的用户分组信息！", ResponseCode.E403);
		}
		
		if(group.getStatus() == 1) {
			group.setStatus(0);
		} else {
			group.setStatus(1);
		}
		this.update(group);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#bindUsergroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void bindUsergroupMapping (String userid , String... groupids) throws BizException {
		if(StringUtils.isEmpty(userid) ||
			groupids  == null || groupids.length == 0) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		
		for (String groupid : groupids) {
			this.storeUserGroupDAO.bindUsergroupMapping(userid, groupid);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#unbindUsergroupMapping(java.lang.String, java.lang.String)
	 */
	public void unbindUsergroupMapping (String userid , String... groupids) throws BizException {
		if(StringUtils.isEmpty(userid) ||
			groupids  == null || groupids.length == 0) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		
		for (String groupid : groupids) {
			this.storeUserGroupDAO.unbindUsergroupMapping(userid, groupid);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#queryStoregroupByCompanyid(java.lang.String)
	 */
	@Override
	public List<StoreUserGroupEntity> queryStoregroupByCompanyid(String companyid) throws BizException {
		if(StringUtils.isEmpty(companyid)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		
		List<StoreUserGroupEntity> groups = this.storeUserGroupDAO.queryStoregroupByCompanyid(companyid);
		return groups;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#queryStroegroupByUserid(java.lang.String)
	 */
	@Override
	public List<StoreUserGroupEntity> queryStroegroupByUserid(String userid) throws BizException {
		if(StringUtils.isEmpty(userid)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		StoreUserEntity user = (StoreUserEntity)this.storeUserService.getEntity(userid);
		if(user == null) {
			throw new BizException("未发现指定的用户。", ResponseCode.E403);
		}
		Set<StoreUserGroupEntity> groups = user.getGroups();
		if(groups == null || groups.size() ==0) {
			return null;
		}
		
		//再次过滤后的信息放在这里
		List<StoreUserGroupEntity> normalGroups = new ArrayList<StoreUserGroupEntity>();
		for (StoreUserGroupEntity group : groups) {
			if(group.getStatus() != 1) {
				continue;
			}
			
			normalGroups.add(group);
		}
		return normalGroups;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserGroupService#queryStroegroupByOrderTypeid(java.lang.String)
	 */
	@Override
	public List<StoreUserGroupEntity> queryStroegroupByOrderTypeid(String ordertypeid) throws BizException {
		if(StringUtils.isEmpty(ordertypeid)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		OrderTypeEntity type = (OrderTypeEntity)this.orderTypeService.getEntity(ordertypeid);
		if(type == null) {
			throw new BizException("没有发现对应的工单类型", ResponseCode.E403);
		}
		
		Set<StoreUserGroupEntity> groupsSet = type.getUserGroups();
		if(groupsSet == null || groupsSet.size() == 0) {
			return null;
		}
		
		//要保证每一次返回的记录都是排序的，但是数据库有没有排序。所以这里要进行拍粗
		List<StoreUserGroupEntity> groups = new ArrayList<StoreUserGroupEntity>(groupsSet);
		Collections.sort(groups, new Comparator<StoreUserGroupEntity>() {
			@Override
			public int compare(StoreUserGroupEntity o1, StoreUserGroupEntity o2) {
				return (int)(o1.getCreateTime().getTime() - o2.getCreateTime().getTime());
			}
		});
		return groups;
	}
}
